﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CE.User;

public partial class user_new_user_activate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.Params["authkey"] != null && Request.Params["uid"] != null)
            {
                if (Request.Params["authkey"].Length > 0 && Request.Params["uid"].Length > 0)
                {
                    //lblAutoKey.Text = Request.Params["authkey"].ToString();
                    //lblGUID.Text = Request.Params["uid"].ToString();
                    ViewState["AutoKey"] = Request.Params["authkey"].ToString();
                    ViewState["UID"] = Request.Params["UID"].ToString();
                    Account _account = new Account();
                    AccountDB _accountdb = new AccountDB();
                    _account = _accountdb.GetUserAccountByAuthKey(Request.Params["authkey"].ToString(), Request.Params["uid"].ToString());

                    if (_account != null)
                    {
                        if (!_account.Status)
                        {
                            string _strGuid = Request.Params["UID"].ToString();
                            if (_accountdb.ActivateAccount(_strGuid))
                            {
                                AccountStatusDB _accountstatusdb = new AccountStatusDB();

                                _accountstatusdb.InsertAccountStatus(_account.UID, 400, 1);
                                GetUserInformation(_account);
                                CE.Global.General.CreateUpdateUserLoginCached(Page);
                                _account = null;

                                lblMessage.Text = "You have successfully activated your account. ";
                                lblMessage.Text += "Click <a href='profile.aspx'>Here</a> to view your profile.";
                            }
                            else
                            {
                                lblMessage.Text = "You have failed active your account.";
                            }
                        }
                        else
                        {
                            lblMessage.Text = "Account is activated.";
                        }
                    }
                    else
                    {
                        lblMessage.Text = "Invalid User ID.";
                    }
                }
                else
                {
                    lblMessage.Text = "Invalid User ID.";
                }
            }
            else
            {
                lblMessage.Text = "Please try again.";
            }
        }

    }

    private void GetUserInformation(Account _account)
    {
        DateTime dtNow = new DateTime();
        dtNow = DateTime.Now;
        Session[CE.Global.General.UserId] = _account.UID;
        Session[CE.Global.General.UserTypeId] = _account.UserTypeId;

        Session[CE.Global.General.UserEmail] = _account.Email;
        Session[CE.Global.General.StartTime] = dtNow.ToString();
        bool isProfileComplete = false;
        bool isContactComplete = false;

        ProfileDB _profiledb = new ProfileDB();
        Profile _profile = new Profile();

        _profile = _profiledb.GetProfile(_account.UID);

        if (_profile != null)
        {
            Session[CE.Global.General.UserName] = Server.HtmlDecode(_profile.Name);

            if (_profile.Status.ToLower() == "complete")
            {
                isProfileComplete = true;
            }
            else
            {
                isProfileComplete = false;
            }
        }
        else
        {
            Session[CE.Global.General.UserName] = null;
        }

        if (isProfileComplete && isContactComplete)
        {
            Session[CE.Global.General.UserProfileAndContactComplete] = "1";
        }
        else
        {
            Session[CE.Global.General.UserProfileAndContactComplete] = "0";
        }

    }
}
